<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">标题:</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">前言:</label>
        <div class="layui-input-block">
            <textarea name="preface" lay-verify="required" lay-reqtext="前言不能为空" placeholder="请输入前言" value="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">内容:</label>
        <div class="layui-input-block">
            <textarea name="content" id="content"  style="display: none;" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="${pageContext.request.contextPath}/layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/layui/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'form','layedit', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            tableSelect = layui.tableSelect,
            layedit=layui.layedit,
            $ = layui.$;

            var index1 = layedit.build('content', {
                tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right'],
                height: 200
            });
        // layedit.getText(index1);//获得纯文本内容
        // layedit.getContent(index1);//获得所有内容，包括样式
        // layedit.setContent(index1, txt);//将内容写入文本域

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let content=layedit.getContent(index1);
            if(content==null || content==""){
                layer.msg("内容不能为空",{icon:2})
                return false;
            }
            data.field.content=content;

            $.ajax({
                url:"${pageContext.request.contextPath}/information/addInformation",
                type:"post",
                data:"data="+JSON.stringify(data.field),
                dataType:"text",
                success:(rs)=>{
                    if(rs=="OK"){
                        layer.msg("添加成功",{icon:1,time:500},function (){
                            parent.location.reload();
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        })
                    }else{
                        layer.msg("添加失败",{icon:2,time:500})
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>